package com.hp.appfw.service.cust;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.hp.appfw.data.mapper.mygen.BaseUserMapper;
import com.hp.appfw.data.mode.mygen.BaseMessageLog;
import com.hp.appfw.data.mode.mygen.BaseMessageMission;
import com.hp.appfw.data.mode.mygen.BaseUser;
import com.hp.appfw.util.SpringUtil;

@Service("mailMessageService")
public class MailMessageService {
	
	@Resource
	MessageCenterService messageService;

	public void mailMessage(String entId, String title, String content, String senderId, List<String> userIdList)
	{
		try {
			BaseMessageMission mission = messageService.createEmailMission(title, content, entId);
			List<BaseMessageLog> logList = new ArrayList<BaseMessageLog>();
			for(String userId : userIdList)
			{
				BaseUserMapper mapper = (BaseUserMapper)SpringUtil.getBeanFactory().getBean(BaseUserMapper.class);
				BaseUser user = mapper.selectByPrimaryKey(userId);
				if(user != null)
				{
					BaseMessageLog log = new BaseMessageLog();
					log.setMissionId(mission.getId());
					log.setReciever(user.getEmail());
					log.setRecieverId(user.getId());
					log.setSenderId(senderId);
					log.setType("Email");
					log.setParam("<none/>");
					logList.add(log);
				}
			}
			messageService.sendMassMessage(mission, logList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
